//
//  ESGreatTopicVC.m
//  Eyeshot
//
//  Created by admin on 15/7/1.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#import "ESGreatTopicVC.h"
#import "ESDuanziVC.h"
#import "ESDuanziCell.h"
#import "ESDuanziVC.h"
#import "ESPhotoCell.h"
#import "ESPhotoTableViewController.h"
#import "ESVideoCell.h"
#import "ESAllTableViewController.h"
#import "ESAllCell.h"
#import "ESProductCell.h"
#import "ProductViewController.h"
@interface ESGreatTopicVC ()

@end

@implementation ESGreatTopicVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.collectionView registerClass:[ESPhotoCell class] forCellWithReuseIdentifier:@"photoCell"];
    [self.collectionView registerClass:[ESVideoCell class] forCellWithReuseIdentifier:@"videoCell"];
    [self.collectionView registerClass:[ESDuanziCell class] forCellWithReuseIdentifier:@"duanziCell"];
    [self.collectionView registerClass:[ESProductCell class] forCellWithReuseIdentifier:@"productCell"];
    [self.collectionView registerClass:[ESPhotoCell class] forCellWithReuseIdentifier:@"greatPhotoCell"];
    [self.collectionView registerClass:[ESAllCell class] forCellWithReuseIdentifier:@"allGreatCell"];
    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"CollectionCell"];
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.item) {
   
            
        case 0: {
            ESAllCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"allGreatCell" forIndexPath:indexPath];
            ESAllTableViewController *allCellVC = [[ESAllTableViewController alloc] initWithStyle:UITableViewStylePlain];
            allCellVC.type = @"list";
            cell.allVC = allCellVC;
            cell.backgroundColor = [UIColor redColor];
            [self addScreenEdgeGestureWithUIRectEdge:UIRectEdgeLeft action:@selector(showLeftMenu) forCell:cell];
            [self addChildViewController:allCellVC];
            return cell;
        }
            break;

        case 1: {
            ESVideoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"videoCell" forIndexPath:indexPath];
            ESVideoVC *videoVc = [[ESVideoVC alloc] initWithStyle:UITableViewStylePlain];
            videoVc.type = @"list";
            cell.videoVC = videoVc;
            [self addChildViewController:videoVc];
            return cell;
        }
            break;
            
        case 2: {
            
            ESPhotoCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"greatPhotoCell" forIndexPath:indexPath];
            
            ESPhotoTableViewController *photoVc = [[ESPhotoTableViewController alloc] initWithStyle:UITableViewStylePlain];
            photoVc.type = @"list";
            cell.photoVC = photoVc;
            [self addChildViewController:photoVc];
            return cell;
            
        }
            break;

        case 3: {
            ESDuanziCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"duanziCell" forIndexPath:indexPath];
            cell.backgroundColor = ESRandomColor;
            ESDuanziVC *duanziVc = [[ESDuanziVC alloc] initWithStyle:UITableViewStylePlain];
            duanziVc.duanziType = @"list";
            cell.tableVc = duanziVc;
            [self addChildViewController:duanziVc];
            return cell;
        }
            break;
        case 4: {
            ESProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"productCell" forIndexPath:indexPath];
            cell.backgroundColor = ESRandomColor;
            ProductViewController *productVc = [[ProductViewController alloc] init];
            cell.vc = productVc;
            [self addScreenEdgeGestureWithUIRectEdge:UIRectEdgeRight action:@selector(showRightMenu) forCell:cell];
            [self addChildViewController:productVc];
            return cell;
        }
            break;
        default: {
            UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
            cell.backgroundColor = ESRandomColor;
            return cell;
        }
            break;
    }
    
    
    
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [super scrollViewDidScroll:scrollView];
    CGPoint offset = scrollView.contentOffset;
    
    NSInteger index = offset.x  / SCREEN_WIDTH;
    if (index != 1 || index != 0) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"remove" object:nil];
        
    }
}

- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
    
    switch (indexPath.item) {
        case 0:
            [self removeChildVCWithClass:[ESAllTableViewController class]];
            break;
        case 1:
            [self removeChildVCWithClass:[ESVideoVC class]];
            break;
        case 2:
            [self removeChildVCWithClass:[ESPhotoTableViewController class]];
            break;
        case 3:
            [self removeChildVCWithClass:[ESDuanziVC class]];
            break;
        default:
            break;
    }
}

- (void)removeChildVCWithClass:(Class)vcClass{
    for (UIViewController *vc in self.childViewControllers) {
        if ([vc isKindOfClass:vcClass]) {
            [vc.view removeFromSuperview];
            [vc removeFromParentViewController];
        }
    }
}


- (void)addScreenEdgeGestureWithUIRectEdge:(UIRectEdge)rectEdgeType action:(SEL)action forCell:(UICollectionViewCell*)cell {
    
    NSArray *array = cell.gestureRecognizers;
    for (UIGestureRecognizer *gr in array) {
        if ([gr isKindOfClass:[UIScreenEdgePanGestureRecognizer class]]) {
            return;
        }
    }
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:action];
    screenEdgePan.edges = rectEdgeType;
    [cell addGestureRecognizer:screenEdgePan];
    
    
}


- (void)showLeftMenu {
    if ([self.delegate respondsToSelector:@selector(didExceedLeftBoundaryOfTopicVC:)]) {
                   [self.delegate didExceedLeftBoundaryOfTopicVC:self];
    }
}

- (void)showRightMenu {
    if ([self.delegate respondsToSelector:@selector(didExceedRightBoundaryOfTopicVC:)]) {
        [self.delegate didExceedRightBoundaryOfTopicVC:self];
    }
}

@end
